import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_module_sale/sale/sale_in/following_record/bean/history_record_bean.dart';
import 'package:flutter/material.dart';

class StepColumnWidget extends StatefulWidget {
  final HistoryRecordBean historyRecordBean;
  final Color topColor;
  final Color centerColor;
  final Color bottomColor;
  final Color textColor;
  final Function itemClick;

  static const Color lineColor = Color(0xFFDFDFDF);
  static const Color circleColor = Color(0xFF999999);

  StepColumnWidget(this.historyRecordBean,
      {this.topColor = lineColor,
        this.centerColor = circleColor,
        this.bottomColor = lineColor,
        this.textColor = Colors.black,
        this.itemClick,
      });

  @override
  StepColumnState createState() => StepColumnState();
}

class StepColumnState extends State<StepColumnWidget> {
  double itemHeight = 0.0;
  List<GlobalKey> _keys;
  List itemHeights = List();

  @override
  void initState() {
    super.initState();
    if(objNotEmpty(widget.historyRecordBean) && objNotEmpty(widget.historyRecordBean.list) &&
       listNotEmpty(widget.historyRecordBean.list)) {
      _keys = List<GlobalKey>.generate(
        widget.historyRecordBean.list.length,
            (int i) => GlobalKey(),
      );
      WidgetsBinding widgetsBinding = WidgetsBinding.instance;
      for (int i = 0; i < widget.historyRecordBean.list.length; i++) {
        widgetsBinding.addPostFrameCallback((callback) {
          ///获取相应控件的size
          RenderObject renderObject = _keys[i].currentContext.findRenderObject();
          setState(() {
            itemHeights.insert(i, renderObject.semanticBounds.size.height);
          });
        });
      }
    }
    ///监听是否渲染完
  }

  Widget _getItemsWidget(int index) {
    return Container(
      key: _keys[index],
      color: Colors.white,
      padding: EdgeInsets.only(left: 12, right: 16),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ///左侧的线
          Container(
            width: 3,
            margin: EdgeInsets.only(right: 10),
            height: index < itemHeights.length ? itemHeights[index] : 0,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  width: 1,
                  height: 10,
                  color: index == 0 ? Colors.transparent : widget.topColor,
                ),
                Container(
                  height: 3,
                  width: 3,
                  decoration: BoxDecoration(
                    color: widget.centerColor,
                    borderRadius: BorderRadius.all(Radius.circular(3)),
                  ),
                ),
                Expanded(
                    child: Container(
                  width: 1,
                  color: index == widget.historyRecordBean.list.length - 1
                      ? Colors.transparent
                      : widget.bottomColor,
                )),
              ],
            ),
          ),

          ///右侧的文案
          Expanded(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text(
                      widget.historyRecordBean.list[index].userName != null
                          ? widget.historyRecordBean.list[index].userName
                          : '',
                      style: TextStyle(
                          fontSize: 15,
                          color: Color(0xFF333333),
                          fontWeight: FontWeight.bold),
                    ),
                 /*   Container(
                      margin: EdgeInsets.only(left: 10),
                      child: Text(
                        widget.historyRecordBean.list[index].subTitle != null
                            ? widget.historyRecordBean.list[index].subTitle
                            : '',
                        style: TextStyle(
                            fontSize: 15,
                            color: Color(0xFF333333),
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                    */
                    Expanded(
                        child: GestureDetector(
                      onTap: () {
                        if (widget.itemClick != null) {
                          widget.itemClick(index);
                        }
                      },
                      child: Text(
                        "查看详情",
                        textAlign: TextAlign.right,
                        style: TextStyle(fontWeight: FontWeight.w500,fontSize:12,color: Color(0xff666666)),
                      ),
//                      padding: EdgeInsets.only(right: 16),
                    )),
                  ],
                ),
                SizedBox(
                  height: 12,
                ),
                Container(
                  child: Text(widget.historyRecordBean.list[index].createdAt != null
                      ? widget.historyRecordBean.list[index].createdAt
                      : '',style: TextStyle(color: Color(0xff666666),fontSize: 12),),
                ),
              /*  SizedBox(
                  height: 7,
                ),
                Container(
                  child: Text(widget.historyRecordBean.list[index].remark != null
                      ? widget.historyRecordBean.list[index].remark
                      : ''),
                ),
                */
                SizedBox(
                  height: 32,
                ),

              ],
              crossAxisAlignment: CrossAxisAlignment.start,
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> widgetList = List();
    if(objEmpty(widget.historyRecordBean)) {
       return Container();
    }
    widgetList.add(Container(
      height: 48,
      padding: EdgeInsets.only(left: 16),
      child: Row(
        children: <Widget>[
          Text(widget.historyRecordBean.title??"历史记录",textAlign: TextAlign.left,
            style: TextStyle(
              color: Color(0xff666666),
              fontSize: 14,
              fontWeight: FontWeight.normal
            ),
          ),
          Spacer()
        ],
      ),
    ));
    if(objNotEmpty(widget.historyRecordBean.list) && listNotEmpty(widget.historyRecordBean.list)) {
      for (int i = 0; i < widget.historyRecordBean.list.length; i++) {
        widgetList.add(_getItemsWidget(i));
      }
    }
    return Column(
      children: widgetList,
    );
  }
}
